import utils from "../utils";

const services = {
    product: {
        query: () => {
            return utils.storage.json("products") || [];
        },
        add: (product) => {
            utils.storage.json("products", services.product.query().concat(product));
        },
        edit: (product) => {
            let products = services.product.query();
            products.forEach((p) => {
                if (p.id === product.id) {
                    p.name = product.name;
                    p.price = product.price;
                    return false;
                }
            });
            utils.storage.json("products", products);
        },
        del: (id) => {
            utils.storage.json("products", services.product.query().filter(item => item.id !== id));
        },
        detail: (id) => {
            let product = {};
            services.product.query().forEach((p) => {
                if (id === p.id) {
                    product = p;
                    return false;
                }
            });
            return product;
        }
    }
};

export default services;